package com.carkeeper.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carkeeper.dto.ServiceSubtypeDTO;
import com.carkeeper.pojo.ServiceSubtypePO;
import com.carkeeper.service.ServiceSubTypeService;
import com.carkeeper.vo.Pager;
import com.carkeeper.vo.ServiceSubTypeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/serviceSubtype")
@CacheConfig(cacheNames = "serviceSubtype")
public class ServiceSubtypeController {
    @Autowired
    private ServiceSubTypeService serviceSubtypeService;

    @Autowired
    private RedisTemplate redisTemplate;

    /*
     * 获取2级分类分页数据 GET/api/serviceSubtype
     *
     * */
    @GetMapping
    public Pager<ServiceSubtypePO> queryPage(@RequestParam Long parentId,
                                             @RequestParam(value = "page", defaultValue = "1") Long page,
                                             @RequestParam(value = "pageSize", defaultValue = "10") Long pageSize,
                                             @RequestParam(value = "code", required = false) String code,
                                             @RequestParam(value = "name", required = false) String name) {
        Page<ServiceSubtypePO> poPage = serviceSubtypeService.queryPage(parentId, page, pageSize, code, name);
        return new Pager(poPage);

    }

    /*
     * 获取2级分类详情  GET/api/serviceSubtype/{id}
     * */
    @GetMapping("/{id}")
    @Cacheable(cacheNames = "query", key = "#id", unless = "#result == null")
    public ServiceSubtypePO query(@PathVariable long id) {
        ServiceSubtypePO serviceSubtypePO = serviceSubtypeService.query(id);
        return serviceSubtypePO;
    }

    /*
     * 新增服务2级分类  POST/api/serviceSubtype
     * */
    @PostMapping
    public boolean add(@RequestBody ServiceSubTypeVo serviceSubTypeVo) {
        ServiceSubtypePO po = new ServiceSubtypePO();
        BeanUtils.copyProperties(serviceSubTypeVo, po);
        serviceSubtypeService.add(po);

        return true;
    }

    /*
     * 更新服务2级分类  PUT/api/serviceSubtype/{id}
     * */
    @PutMapping("/{id}")
    @CacheEvict(cacheNames = "query", key = "#id")
    public boolean update(@PathVariable Long id,
                          @RequestBody ServiceSubtypePO serviceSubtypePO) {
        serviceSubtypeService.update(id, serviceSubtypePO);
        return true;
    }

    /*
     *删除服务2级分类   DELETE/api/serviceSubtype/{id}
     * */
    @DeleteMapping("/{id}")
    @CacheEvict(cacheNames = "query", key = "#id")
    public boolean delete(@PathVariable long id) {
        serviceSubtypeService.delete(id);
        return true;
    }

    /**
     * 获取服务二级分类列表
     *
     * @return 服务二级分类列表
     */
    @GetMapping("/list")
    public List<ServiceSubtypeDTO> findAll() {
        return serviceSubtypeService.findAll().stream().map(po -> {
            ServiceSubtypeDTO dto = new ServiceSubtypeDTO();
            BeanUtils.copyProperties(po, dto);
            return dto;
        }).collect(Collectors.toList());
    }
}
